//
//  Today_Today_detail_MusicTableViewCell.m
//  Time
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "Today_Today_detail_MusicTableViewCell.h"


@interface Today_Today_detail_MusicTableViewCell ()
/**
 *  圆形播放图
 */
@property (nonatomic,retain) UIImageView *roundImageView;
/**
 *  播放按钮
 */
@property (nonatomic,retain) UIButton *playButton;
/**
 *  标题
 */
@property (nonatomic,retain) UILabel *titleLabel;
/**
 *  文本内容
 */
@property (nonatomic,retain) UILabel *contentLabel;
/**
 *  用户头像
 */
@property (nonatomic,retain) UIImageView *iconImageView;
/**
 *  用户名
 */
@property (nonatomic,retain) UILabel *unameLabel;
/**
 *  判断是否在播放
 */
@property (nonatomic,assign) BOOL isPlay;
@end

@implementation Today_Today_detail_MusicTableViewCell

-(void)dealloc{
    [_roundImageView release];
    [_playButton release];
    [_titleLabel release];
    [_contentLabel release];
    [_iconImageView release];
    [_unameLabel release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.isPlay = NO;
        
        self.roundImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.roundImageView];
        [_roundImageView release];
        
        self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.playButton];
        [_playButton release];
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        [_titleLabel release];
        
        self.contentLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.contentLabel];
        [_contentLabel release];
        
        self.iconImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.iconImageView];
        [_iconImageView release];
        
        self.unameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.unameLabel];
        [_unameLabel release];
        
//        SCBAVPlayer *scbAVPlayer = [SCBAVPlayer shareSCBAVPlayer];
        [self handleMusicDate];
    }
    return self;
}

-(void)handleMusicDate{
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.roundImageView.layer.cornerRadius = SWIDTH * 0.25;
    self.roundImageView.layer.masksToBounds = YES;
    self.roundImageView.layer.borderWidth = 10;
    [self.roundImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView.mas_centerX).offset(0);
        make.top.equalTo(self.contentView.mas_top).offset(50);
        make.width.mas_equalTo(SWIDTH * 0.5);
        make.height.mas_equalTo(SWIDTH * 0.5);
    }];
    
    self.playButton.layer.cornerRadius = SWIDTH * 0.05;
    self.playButton.layer.masksToBounds = YES;
    [self.playButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.roundImageView.mas_centerX).offset(0);
        make.centerY.equalTo(self.roundImageView.mas_centerY).offset(0);
        make.width.mas_equalTo(SWIDTH * 0.1);
        make.width.mas_equalTo(SWIDTH * 0.1);
    }];
    [self.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:24];
    self.titleLabel.numberOfLines = 0;
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(20);
        make.right.equalTo(self.contentView.mas_right).offset(-20);
        make.top.equalTo(self.roundImageView.mas_bottom).offset(50);
        make.height.mas_equalTo([Tools getTextHeight:self.model.title withWidth:SWIDTH - 40 withFontSize:24]);
    }];
    
    
    self.contentLabel.textColor = [UIColor lightGrayColor];
    self.contentLabel.font = [UIFont systemFontOfSize:12.0];
    self.contentLabel.numberOfLines = 0;
    [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(20);
        make.right.equalTo(self.contentView.mas_right).offset(-20);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(30);
        make.height.mas_equalTo([Tools getTextHeight:self.model.text withWidth:SWIDTH - 40 withFontSize:12]);
    }];
    
    self.iconImageView.layer.cornerRadius = SWIDTH * 0.05;
    self.iconImageView.layer.masksToBounds = YES;
    [self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(20);
//        make.right.equalTo(self.contentView.mas_right).offset(-20);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(20);
        make.width.mas_equalTo(SWIDTH * 0.1);
        make.height.mas_equalTo(SWIDTH * 0.1);
    }];

    self.unameLabel.textColor = [UIColor whiteColor];
    [self.unameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(10);
        make.width.mas_equalTo(SWIDTH * 0.3);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(SWIDTH * 0.1);
    }];
}

-(void)setModel:(SCB_Today_Detail_Model *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    [self.roundImageView setImageWithURL:[NSURL URLWithString:model.scbSongInfo.albumcover] placeholderImage:nil];
    [self.playButton setImage:[UIImage imageNamed:@"播放 (3).png"] forState:UIControlStateNormal];
    self.titleLabel.text = model.title;
    self.contentLabel.text = model.text;
    [self.iconImageView setImageWithURL:[NSURL URLWithString:model.scbUserinfo.icon] placeholderImage:nil];
    self.unameLabel.text = model.scbUserinfo.uname;
    SCBAVPlayer *scbAVPlayer = [SCBAVPlayer shareSCBAVPlayer];
    NSURL *urlMusic = [NSURL URLWithString:@"http://m5.file.xiami.com/62/70062/365501/1769338879_1034347_l.mp3?auth_key=d9a50140ae58465c57190dcb0548b1b2-1469750400-0-null"];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:urlMusic];
    [scbAVPlayer.AVPlayer replaceCurrentItemWithPlayerItem:item];
    NSLog(@"songId:%@",model.songid);
    NSLog(@"songid:%@",model.scbSongInfo.songid);
    NSLog(@"$id:%@",model.scbSongInfo.key_id);
}

-(void)playButtonAction:(UIButton *)button{
    
    if (self.isPlay == NO) {
        SCBAVPlayer *scbAVPlayer = [SCBAVPlayer shareSCBAVPlayer];
        [scbAVPlayer.AVPlayer play];
        self.isPlay = !self.isPlay;
        NSLog(@"开始");
        [self.playButton setImage:[UIImage imageNamed:@"暂停 (1).png"] forState:UIControlStateNormal];
    } else {
        SCBAVPlayer *scbAVPlayer = [SCBAVPlayer shareSCBAVPlayer];
        [scbAVPlayer.AVPlayer pause];
        self.isPlay = !self.isPlay;
        NSLog(@"暂停");
        [self.playButton setImage:[UIImage imageNamed:@"播放 (3).png"] forState:UIControlStateNormal];
    }
    
}

@end
